package cn.john.basic.service.impl;

import cn.john.basic.mapper.BaseMapper;
import cn.john.basic.query.BaseQuery;
import cn.john.basic.service.IBaseService;
import cn.john.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 基础的公共接口的实现类
 * @param <T>
 */
/* @Service : 应该交给子类继承(所以子类才应该交由容器管理)，这里不应该交由容器管理 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional()
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    @Override
    public PageList queryPage(BaseQuery query) {
        // 查询出总条数
        Integer total = mapper.loadCount(query);
        // 查询出每页数据
        List<T> rows = mapper.loadPage(query);
        return new PageList(total,rows);
    }

    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        mapper.patchDelete(ids);
    }
}
